package com.strive.config;

import com.strive.security.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @Author: strivePeng
 * @CreateTime: 2024年03月18日
 * @Description: 配置security的核心配置
 **/
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    // login异常
    @Autowired
    LoginFailureHandler loginFailureHandler;
    // login正常
    @Autowired
    LoginSuccessHandler loginSuccessHandler;

    // 注入验证码图片自定义的过滤器
    @Autowired
    CaptchaFilter captchaFilter;

    // 注入入口
    @Autowired
    JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;

    // 注入异常处理器
    @Autowired
    JwtAccessDeniedHandler jwtAccessDeniedHandler;

    // 注入用户登录查库service
    @Autowired
    UserDetailServiceImpl userDetailService;

    // 引入退出处理器
    @Autowired
    JwtLogoutSuccessHandler jwtLogoutSuccessHandler;

    // 注入JWT拦截器
    @Bean
    JwtAuthenticationFilter jwtAuthenticationFilter() throws Exception {
        JwtAuthenticationFilter jwtAuthenticationFilter = new JwtAuthenticationFilter(authenticationManager());
        return jwtAuthenticationFilter;
    }

    // 拦截白名单的配置
    private static final String[] URL_WHITELIST = {"/login", "/logout", "/captcha", "/favicon.ioc"};

    // 密码加密形式
    @Bean
    BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    protected void configure(HttpSecurity http) throws Exception {
        // 1 配置跨域
        http.cors().and()
                // 2 关闭预防攻击
                .csrf().disable()
                // 3 登录配置
                .formLogin()
                // 3.2 认证成功处理器
                .successHandler(loginSuccessHandler)
                // 3.3 认证失败处理器
                .failureHandler(loginFailureHandler)
                // 3.4 退出处理器
                .and()
                .logout()
                .logoutSuccessHandler(jwtLogoutSuccessHandler)
                // 4 禁用session 配置生成规则
                .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                // 5 配置拦截规则
                .and()
                // 6 设置白名单
                .authorizeRequests().antMatchers(URL_WHITELIST).permitAll()
                // 7 其余所有链接都要登录后访问
                .anyRequest().authenticated()

                // 8 异常处理器
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(jwtAuthenticationEntryPoint) // 配置入口
                .accessDeniedHandler(jwtAccessDeniedHandler) // 权限不足的处理器

                // 9 自定义过滤器的配置 在用户名密码验证之前调用
                .and().addFilter(jwtAuthenticationFilter()).addFilterBefore(captchaFilter, UsernamePasswordAuthenticationFilter.class);
    }

    /**
     * 配置 Service
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailService);
    }
}
